package com.soft.javase.day10.practice.stream2;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author yzh
 * @desc TODO
 * @date 2025/5/4
 */
public class StreamPractice {
    public static void main(String[] args) {
        System.out.println("of示例：");
        Stream.of("A", "B", "C").forEach(System.out::println);

        System.out.println("\niterate示例：");
        Stream.iterate(1, n -> n * 2)
                .limit(5)
                .forEach(System.out::println);

        System.out.println("\ngenerate示例：");
        Stream.generate(Math::random)
                .limit(3)
                .forEach(System.out::println);

        System.out.println("\nfindFirst示例：");
        Optional<String> first = Stream.of("A", "B", "C").findFirst();
        System.out.println(first.get());

        System.out.println("\nfindAny示例：");
        Optional<String> any = Stream.of("A", "B", "C").findAny();
        System.out.println(any.get());

        System.out.println("\nanyMatch示例：");
        boolean hasEven = Stream.of(1, 4, 7, 3, 10, 7).anyMatch(n -> n % 2 == 0);
        System.out.println(hasEven);

        System.out.println("\ndistinct示例：");
        Stream.of(1, 2, 2, 3, 3, 3)
                .distinct()
                .forEach(System.out::println);

        System.out.println("\n筛选出Integer集合中大于7的元素：");
        List<Integer> numbers = Arrays.asList(6, 7, 3, 8, 1, 2, 9);
        numbers.stream()
                .filter(n -> n > 7)
                .forEach(System.out::println);

        System.out.println("\n筛选员工中工资高于8000的人，并形成新的集合");
        List<Employee> employees = Arrays.asList(
                new Employee("张三", 28, 7500.0),
                new Employee("张明", 36, 9500.0),
                new Employee("李四", 36, 8500.0),
                new Employee("王五", 38, 9500.0),
                new Employee("赵六", 35, 8500.0)
        );

        List<Employee> resultEmployee = employees.stream()
                .filter(e -> e.getSalary() > 8000.0)
                .collect(Collectors.toList());

        resultEmployee.forEach(e -> System.out.println(e.getName()));

        System.out.println("\n获取String集合中最长的元素");
        List<String> strings = Arrays.asList("Java", "Python", "C++", "JavaScript");
        String s = strings.stream().reduce((s1, s2) -> s1.length() > s2.length() ? s1 : s2).orElse("");
        System.out.println(s);

        System.out.println("\n获取Integer集合中最大值");
        List<Integer> nums = Arrays.asList(1, 5, 3, 9, 2);
        Integer max = nums.stream().reduce(Integer::max).orElse(0);
        System.out.println(max);

        System.out.println("\n获取员工工资最高的人");
        Employee employee = employees.stream().reduce((e1, e2) -> e1.getSalary() > e2.getSalary() ? e1 : e2).orElse(null);
        System.out.println(employee.getName());

        System.out.println("\n计算Integer集合中大于6的元素的个数");
        System.out.println(numbers.stream()
                .filter(n -> n > 6)
                .count());

        System.out.println("\n将员工按照工资由低到高排序，显示名字");
        employees.stream()
                .sorted(Comparator.comparingDouble(Employee::getSalary))
                .map(Employee::getName)
                .forEach(System.out::println);

        System.out.println("\n将员工按照工资由高到低排序，显示名字");
        employees.stream()
                .sorted(Comparator.comparingDouble(Employee::getSalary).reversed())
                .map(Employee::getName)
                .forEach(System.out::println);

        System.out.println("\n将员工按照工资由低到高（工资一样则按年龄由小到大）排序，显示名字");
        employees.stream()
                .sorted(Comparator.comparingDouble(Employee::getSalary)
                        .thenComparingInt(Employee::getAge))
                .map(Employee::getName)
                .forEach(System.out::println);
    }
}
